import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/common/api.js'
Vue.use(Vuex)
const store = new Vuex.Store({
	state: {
		userInfo: {},
		cityInfo: {
			city: '',
			latitude: '',
			longitude: ''
		},
		manObj: {

		},
		currency: '',
		lang: ''
	},
	getters: {

	},
	mutations: {
		changeUserInfo(state, data) {
			state.userInfo = data
		},
		addCityInfo(state, data) {
			state.cityInfo = data
		},
		addManObj(state, data) {
			state.manObj = data
		},
		saveCurrency(state, data) {
			state.currency = data
		},
		saveLang(state, data) {
			state.lang = data.lang
		},
	},
	actions: {
		getuserInfo(context) {
			api.userInfo().then((res) => {
				if (res && res.code == 1) {
					var hash = window.location.hash
					var a = hash.slice(1)
					var b = a.indexOf('?')
					let  result 
					if(b!=-1){
						result = a.slice(0, b)
					}else{
						result = a
					}
					
					let perArr = [{
						hash: '/pages/index/arrange/arrange',
						name: 'level',
					}, {
						hash: '/pages/mine/team-list',
						name: 'is_partner',
					}, {
						hash: '/pages/mine/teame-detail',
						name: 'is_partner',
					}, {
						hash: '/pages/mine/goodList',
						name: 'gradelevel',
					}, {
						hash: '/pages/mine/good-detail',
						name: 'gradelevel',
					}, {
						hash: '/pages/mine/good-order',
						name: 'gradelevel',
					}, {
						hash: '/pages/mine/order-win',
						name: 'gradelevel',
					}, {
						hash: '/pages/mine/order-fans',
						name: 'gradelevel',
					}, {
						hash: '/pages/mine/order-list',
						name: 'gradelevel',
					}, {
						hash: '/pages/mine/order-detail',
						name: 'gradelevel',
					}, {
						hash: '/pages/mine/logInfo',
						name: 'gradelevel',
					}, {
						hash: "/pages/address/newAddress",
						name: 'gradelevel',
					}, {
						hash: "/pages/address/addressDetail",
						name: 'gradelevel',
					}]
					
					perArr.forEach((item, index) => {
						if (result == item.hash) {
							if (item.name == 'level') {
								if (!(res.data.level > 2)) {
									uni.reLaunch({
										url: '/pages/login/Nopermission'
									})
								}
							} else if (item.name == 'is_partner') {
								if (res.data.is_partner != 1) {
									uni.reLaunch({
										url: '/pages/login/Nopermission'
									})
								}
							} else if (item.name == 'gradelevel') {
								if (!(res.data.grade >= 1 && res.data.level >= 3)) {
									uni.reLaunch({
										url: '/pages/login/Nopermission'
									})
								}
							}
						}
					})
					context.commit("changeUserInfo", res.data)
				}
			})
		},
		changeCityInfo(context, data) {
			let datas = uni.getStorageSync('cityInfo') || '';
			context.commit('addCityInfo', datas)
		},
		changeMan(context, data) {
			context.commit('addManObj', data)
		},
		// 币种
		changecCrrency(context, data) {
			let datas = uni.getStorageSync('currency') || '';
			context.commit('saveCurrency', datas)
		},
		// 语言
		changecLang(context, data) {
			let datas = uni.getStorageSync('lang') || '';
			context.commit('saveLang', datas)
		}
	}
})

export default store